import {
	observable,
	action,
} from 'mobx';
import {
	Form,
} from 'weaver-mobile-page';
import {
	Bubble,
	Tools,
} from 'weaver-mobile';

import * as api from '../../apis/groupAdvice.js';
const getLabel = Tools.getLabel;

export class GroupAdviceStore {
	groupId = "";
	@observable form = new Form.Store({
		route: '/groupAdvice',
	});

	@action getForm = () => {
		const params = {
			groupId: this.groupId
		}
		api.getGroupSuggestForm(params).then(datas => {
			const {api_status, conditions} = datas;
			if (api_status) {
				this.form.initForm(conditions, '/groupAdvice');
			}else{
				Bubble.warning(datas.message || "error");
			}
		})
	}

	save = () => {
		// const result = this.form.doValidate();
		const { suggestTitle, content } = this.form.formParams;

		if (!suggestTitle) {
		    Bubble.warning(getLabel(521678, "标题未填写"));
		    return;
		}

		if (!content) {
		    Bubble.warning(getLabel(521679, "需添加/删除的成员未填写"));
		    return;
		}


		const params = {
			groupId: this.groupId,
			...this.form.formParams
		};
		api.saveGroupSuggest(params).then(datas => {
			const {sign, message} = datas;

			if (sign == "1") {
				Bubble.success(message);
				this.closeWindow();
			}else{
				Bubble.warning(message);
			}
		})
	}

	closeWindow= () =>{
		if (window.em && window.em.checkJsApi('closeWindow')) {
			window.em.closeWindow({
				fail: (msg) => {
					Bubble.error(JSON.stringify(msg))
				}
			});
		}
	}

	setGroupId = (id) => {
		this.groupId = id;
	}
}

